package com.andy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.andy.service.IManageTestService;
import com.andy.vo.CommonMessageResultVO;
import com.andy.vo.ExamSubjectVO;
import com.andy.vo.ExamTestTypeVO;
import com.andy.vo.ExamTestVO;

@Controller
@RequestMapping(value = "/teacher")
public class ManageTestController {

	@Autowired
	private IManageTestService manageTestService;

	@RequestMapping(value = "/toManageTest")
	public String toManageTest(Model model) {
		List<ExamTestVO> testList = manageTestService.loadAllTest();
		model.addAttribute("testList", testList);
		return "teacher/manageTest";
	}
	
	@ResponseBody
	@RequestMapping(value = "/getSubjectAndTypes")
	public Map<String, Object> getSubjectAndTypes() {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<ExamSubjectVO> subjectList = manageTestService.getSubjectList();
		List<ExamTestTypeVO> testTypeList = manageTestService.getTestTypeList();
		
		resultMap.put("subjectList", subjectList);
		resultMap.put("testTypeList", testTypeList);
		return resultMap;
	}
	
	@ResponseBody
	@RequestMapping(value = "/saveTest", method = RequestMethod.POST)
	public CommonMessageResultVO saveTest(@Valid ExamTestVO examTestVO, BindingResult bindingResult) {
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String, String>();
		resultVO.setSuccess(false);
		if (bindingResult.hasErrors()) {
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for (ObjectError error : errorList) {
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			resultVO.setMessageMap(messageMap);
			return resultVO;
		}
		try {
			manageTestService.addTest(examTestVO);
			resultVO.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/updateTest", method = RequestMethod.POST)
	public CommonMessageResultVO updateTest(@Valid ExamTestVO examTestVO, BindingResult bindingResult) {
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String, String>();
		resultVO.setSuccess(false);
		if (bindingResult.hasErrors()) {
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for (ObjectError error : errorList) {
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			resultVO.setMessageMap(messageMap);
			return resultVO;
		}
		try {
			manageTestService.updateTest(examTestVO);
			
//			manageTestService.createTest();
			resultVO.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/deleteTest", method = RequestMethod.POST)
	public CommonMessageResultVO deleteTest(Integer testId) {
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String, String>();
		resultVO.setSuccess(false);
		try {
			manageTestService.deleteTest(testId);
			resultVO.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}

	@ResponseBody
	@RequestMapping(value = "/getTestById", method = RequestMethod.POST)
	public ExamTestVO getTestById(Integer testId) {
		ExamTestVO resultVO = manageTestService.getTestById(testId);
		return resultVO;
	}
}
